//
//  LyTabBar.swift
//  LySwift微博
//
//  Created by 贠明东 on 15/11/23.
//  Copyright © 2015年 itcCZ. All rights reserved.
//

import UIKit

class LyTabBar: UITabBar {

    override func layoutSubviews() {
        super.layoutSubviews()
 
        ///计算Item的宽度
        let width = bounds.size.width / 5
        
        //创建frame
        let frame = CGRect(x: 0, y: 0, width: width, height: bounds.size.height)
        //记录是第几个View
        var index = 0
        //重新设置按钮位置(UITaBarButton)
        for view in self.subviews
        {
            //判断View是否是UIControl的子控件并且View不是UIButton  
            if view is UIControl && !(view is UIButton)
            {
                //发布按钮不需要来设置frame，忽略发布按钮
                //只有系统的UITabBarButton需要设置frame
                //设置frame  CGRectOffset：可以平移frame
                view.frame = CGRectOffset(frame, CGFloat(index) * width, 0)
                
    
//                index++
                
                index += index == 1 ? 2 : 1
            }
        }
        //设置发布按钮的fram
        composeButton.frame = CGRectOffset(frame, width * 2, 0)
    }
    ///懒加载
    lazy var composeButton: UIButton = {
        let button = UIButton()
        
        //设置参数
        //设置发布按钮背景普通图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        //设置发布按钮背景高亮图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //设置发布按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        //设置发布按钮高亮图片
           button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //添加到TabBar里面
        self.addSubview(button)
        
        return button
        }()
}
